package com.sc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.sc.dao.OrderDao;
import com.sc.dao.ShopCartDao;
import com.sc.model.Order;
import com.sc.model.vo.OrderVo;
import com.sc.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private ShopCartDao shopCartDao;

	@Autowired
	private OrderDao orderDao;

	@Override
	public List<OrderVo> getOrdersByUserId(String userId, int page) {
		List<OrderVo> orderVo = new ArrayList<OrderVo>();
		long rowCount = orderDao.getByUserIdCount(userId);
		List<Order> orders = orderDao.getByUserId(userId, (page - 1) * OrderVo.ROWS, OrderVo.ROWS);
		if (!CollectionUtils.isEmpty(orders)) {
			for (Order order : orders) {
				orderVo.add(new OrderVo(order, shopCartDao.getByOrderId(order.getId())));
			}
			orderVo.get(0).setRowCount(rowCount);
			orderVo.get(0).setPage(page);
		}
		return orderVo;
	}

}
